package or.exa.cangguan.service.impl;

import com.google.code.kaptcha.impl.DefaultKaptcha;
import lombok.extern.slf4j.Slf4j;
import or.exa.cangguan.common.Redis;
import or.exa.cangguan.mapper.UserInfoMapper;
import or.exa.cangguan.service.LoginService;
import or.exa.cangguan.utils.DigestUtil;
import or.exa.cangguan.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private DefaultKaptcha kaptcha;
    @Autowired
    private Redis redis;
    @Autowired
    private UserInfoMapper userInfoMapper;
    @Autowired
    private DigestUtil digestUtil;
    @Autowired
    private JWTUtil jwtUtil;


    @Override
    public Map<String,String> VerificationCode() throws IOException {
        String text = kaptcha.createText();
        log.info("验证码: {}", text);
        BufferedImage image = kaptcha.createImage(text);
        String uuid = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
        
        log.info("UUID: {}", uuid);
        log.info("验证码图片: {}", image);


        redis.setExpire(uuid, text);


        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(image, "png", outputStream);
        String base64Image = Base64.getEncoder().encodeToString(outputStream.toByteArray());

        return new HashMap<String, String>() {{
            put("uuid", uuid);
            put("image", base64Image);
        }};

    }

    @Override
    public String login(String username, String password, String verificationCode, String uuid) {

        String code = (String) redis.get(uuid);
//        if (code == null) {
//            log.info("验证码已过期");
//            return 1;
//        }
//        if(code!= verificationCode){
//            log.info("验证码错误");
//            return 2;
//        }

        password= digestUtil.hmacSign(password);
        String UserPassword = userInfoMapper.selectByUsername(username);
        log.info("加密后的密码: {}", password);
        log.info("数据库中的密码: {}", UserPassword);
        log.info("coed:"+code);
        log.info("verificationCode:"+verificationCode);

//        if (UserPassword == null) {
//            log.info("用户名不存在");
//            return 3;
//        }
//        if (!UserPassword.equals(password)) {
//            log.info("密码错误");
//            return 4;
//        }
        if (UserPassword.equals(password)&&code.equals(verificationCode)) {
            log.info("登录成功");
            redis.del(uuid);
            String token = jwtUtil.getToken(new HashMap<String, String>() {{
                put("username", username);
            }});
            log.info("token: {}", token);
            redis.setExpire2(username, token);

//            redis.setExpire2("redis"+uuid, token);

            return token;
        }

        return null;



    }
}
